/**
 * 
 */
package com.sys.object.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @author Administrator
 * 
 */
public class IpUtils {

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if ((ip != null) && (ip.contains("121.101.223.250"))) {
		}

		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip =request.getHeader("X-Real-IP");//
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		return ip;
	}
	
	public static String getIpAddr() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String ip = request.getHeader("x-forwarded-for");
		if ((ip != null) && (ip.contains("121.101.223.250"))) {
		}

		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		if ((ip == null) || (ip.length() == 0)
				|| ("unknown".equalsIgnoreCase(ip))) {
			ip =request.getHeader("X-Real-IP");//
			if ((ip != null) && (ip.contains("121.101.223.250"))) {
			}
		}
		return ip;
	}
	
//
//	/**
//	 * 获取本机在局域网中的IP
//	 * @return
//	 */
//	public static String getLoalIpAddr(HttpServletRequest request) {
//		try {
//			String ips = request.getLocalAddr();
//			String port = ConfigUtils.getStringValue("tomcatPort");//request.getLocalPort();
//			int serverPort = request.getServerPort();
//			String tomcatPort = System.getProperty("file.port");//
//			
//			String encoding = System.getProperty("file.encoding");
//			String abc = System.getProperty("current.environment");
//			Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
//			InetAddress ip = null;
//			while (allNetInterfaces.hasMoreElements()) {
//				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
//				//System.out.println(netInterface.getName());
//				Enumeration addresses = netInterface.getInetAddresses();
//				while (addresses.hasMoreElements()) {
//					ip = (InetAddress) addresses.nextElement();
//					if (ip != null && ip instanceof Inet4Address) {
//						String ipString = ip.getHostAddress();
//						if(ipString.indexOf("192.168")!=-1){
//							//System.out.println(abc+"<<>>"+encoding+"本机的局域网IP = " + ipString+"<<>>"+ips+"<<>>"+port+"<<>>"+serverPort+"<<tomcatPort>>"+tomcatPort);
//							return ips+":"+ipString+":"+port;
//						}
//					}
//				}
//			}
//		} catch (Exception e) {
//			//e.printStackTrace();
//		}
//		return "";
//	}
}
